package com.example.computer_exam_training_sys.service.impl;

import com.example.computer_exam_training_sys.entity.Admin_Feedbackresponse_Info;
import com.example.computer_exam_training_sys.mapper.Admin_feedbackresponseMapper;
import com.example.computer_exam_training_sys.service.Admin_feedbackresponse_Service;
import com.example.computer_exam_training_sys.entity.adminlogsInfo;
import com.example.computer_exam_training_sys.mapper.admin_logsMapper;
import com.example.computer_exam_training_sys.service.admin_logsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
import java.util.List;

@Service
public class Admin_feedbackresponseServiceImpl implements Admin_feedbackresponse_Service {
    @Autowired
    private Admin_feedbackresponseMapper responseMapper;

    @Autowired
    private admin_logsMapper adminLogsMapper;

    @Override
    public int addResponse(Admin_Feedbackresponse_Info response) {
        int result = responseMapper.addResponse(response);
        if (result > 0) {
            // 添加日志记录
            adminlogsInfo log = new adminlogsInfo();
            log.setAdminId(response.getAdminId());
            log.setOperationType(response.getFeedbackType() == 1 ? "完善用户体验" : "回复答题疑惑");
            log.setTargetId(response.getFeedbackId());
            log.setTargetType("反馈");
            log.setOperationDetail(response.getResponseContent());
            log.setIpAddress("127.0.0.1"); // 假设IP地址为本地地址，实际应从请求中获取
            log.setCreateTime(LocalDateTime.ofInstant(Instant.now(), ZoneId.systemDefault()));
            adminLogsMapper.addLog(log);
        }
        return result;
    }

    @Override
    public int updateResponse(Admin_Feedbackresponse_Info response) {
        int result = responseMapper.updateResponse(response);
        if (result > 0) {
            // 添加日志记录
            adminlogsInfo log = new adminlogsInfo();
            log.setAdminId(response.getAdminId());
            log.setOperationType(response.getFeedbackType() == 1 ? "完善用户体验" : "回复答题疑惑");
            log.setTargetId(response.getFeedbackId());
            log.setTargetType("反馈");
            log.setOperationDetail(response.getResponseContent());
            log.setIpAddress("127.0.0.1"); // 假设IP地址为本地地址，实际应从请求中获取
            log.setCreateTime(LocalDateTime.ofInstant(Instant.now(), ZoneId.systemDefault()));
            adminLogsMapper.addLog(log);
        }
        return result;
    }

    @Override
    public int deleteResponse(Integer response_id) {
        return responseMapper.deleteResponse(response_id);
    }

    @Override
    public Admin_Feedbackresponse_Info getResponseById(Integer response_id) {
        return responseMapper.getResponseById(response_id);
    }

    @Override
    public List<Admin_Feedbackresponse_Info> getResponsesByFeedbackId(Integer feedback_id) {
        return responseMapper.getResponsesByFeedbackId(feedback_id);
    }

    @Override
    public List<Admin_Feedbackresponse_Info> findAllResponses() {
        return responseMapper.findAllResponses();
    }

    @Override
    public List<Admin_Feedbackresponse_Info> findAllResponsesByPage(int page, int pageSize) {
        List<Admin_Feedbackresponse_Info> all = responseMapper.findAllResponses();
        int fromIndex = Math.max(0, (page - 1) * pageSize);
        int toIndex = Math.min(fromIndex + pageSize, all.size());
        if (fromIndex > toIndex) return java.util.Collections.emptyList();
        return all.subList(fromIndex, toIndex);
    }

    @Override
    public List<Admin_Feedbackresponse_Info> getResponsesByFeedbackIdByPage(Integer feedback_id, int page, int pageSize) {
        List<Admin_Feedbackresponse_Info> all = responseMapper.getResponsesByFeedbackId(feedback_id);
        int fromIndex = Math.max(0, (page - 1) * pageSize);
        int toIndex = Math.min(fromIndex + pageSize, all.size());
        if (fromIndex > toIndex) return java.util.Collections.emptyList();
        return all.subList(fromIndex, toIndex);
    }

    @Override
    public List<Admin_Feedbackresponse_Info> findAllResponsesWithUser() {
        return responseMapper.findAllResponsesWithUser();
    }

    @Override
    public int countAllResponses() {
        return responseMapper.countAllResponses();
    }

    @Override
    public List<Admin_Feedbackresponse_Info> findAllResponsesWithUserByPage(int page, int pageSize) {
        List<Admin_Feedbackresponse_Info> all = responseMapper.findAllResponsesWithUser();
        int fromIndex = Math.max(0, (page - 1) * pageSize);
        int toIndex = Math.min(fromIndex + pageSize, all.size());
        if (fromIndex > toIndex) return java.util.Collections.emptyList();
        return all.subList(fromIndex, toIndex);
    }
}